package com.example.zhihuhelpapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhihuhelpapp.R;
import com.example.zhihuhelpapp.adapter.AuthorListAdapter;
import com.example.zhihuhelpapp.base.MyBaseRxActivity;
import com.example.zhihuhelpapp.bean.AuthorBean;
import com.example.zhihuhelpapp.bean.QuestionBean;
import com.example.zhihuhelpapp.bean.QuestionResultBean;
import com.example.zhihuhelpapp.event.UIEvent;
import com.example.zhihuhelpapp.mvp.QuestionListPresenter;
import com.example.zhihuhelpapp.mvp.contract.IQuestionListContract;
import com.example.zhihuhelpapp.utils.CoreConstant;
import com.example.zhihuhelpapp.utils.DbUtils;
import com.example.zhihuhelpapp.utils.SpUtils;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import java.util.ArrayList;
import java.util.List;

public class QuestionDetailActivity extends MyBaseRxActivity<QuestionListPresenter>
        implements IQuestionListContract.IView, MultiItemTypeAdapter.OnItemClickListener {
    private static final String KEY_QUESTION_ID = "key_question_id";

    private TextView tv_question_title;
    private TextView tv_question_total_answer_count;
    private TextView tv_my_answer_raking;

    private String questionId;
    private String nickName;
    private int page = 0; // 当前页面
    private int myAnswerRaking = 0;  // 我的回答排名
    private boolean isSuccess = false;  // 是否找到我的回答

    // 我前面的用户
    private List<AuthorBean> authorBeanList;
    private AuthorListAdapter authorListAdapter;

    public static void showQuestionDetail(Activity activity, String questionId) {
        Intent intent = new Intent(activity, QuestionDetailActivity.class);
        intent.putExtra(KEY_QUESTION_ID, questionId);
        activity.startActivity(intent);
    }

    @Override
    protected QuestionListPresenter createPresenter() {
        return new QuestionListPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_question_detail;
    }

    @Override
    protected void initView() {
        showToolBar(true, getString(R.string.question_detail));

        tv_question_title = findViewById(R.id.tv_question_title);
        tv_question_total_answer_count = findViewById(R.id.tv_question_total_answer_count);
        tv_my_answer_raking = findViewById(R.id.tv_my_answer_raking);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        authorBeanList = new ArrayList<>();
        authorListAdapter = new AuthorListAdapter(this, authorBeanList);
        recyclerView.setAdapter(authorListAdapter);

        authorListAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        nickName = SpUtils.getInstance().get(CoreConstant.SpConstant.KEY_NICK_NAME, "");
        questionId = getIntent().getStringExtra(KEY_QUESTION_ID);
        if (TextUtils.isEmpty(questionId) || TextUtils.isEmpty(nickName)) {
            showError(getString(R.string.tip_param_error));
            finish();
        }
        QuestionBean questionBean = DbUtils.getQuestion(questionId);
        if (questionBean != null) {
            tv_question_title.setText(getString(R.string.app_home_question_name, questionBean.getQuestionTitle()));
            tv_question_total_answer_count.setText(getString(R.string.app_home_answer_count, questionBean.getQuestionAnswerCount()));
            tv_my_answer_raking.setText(getString(R.string.question_answer_raking, questionBean.getAnswerRaking()));
        }
        getQuestionDetail();
    }

    private void getQuestionDetail() {
        mPresenter.getQuestionDetail(questionId, page);
    }

    @Override
    public void onSaveQuestionSuccess() {

    }

    @Override
    public void onDelQuestionSuccess() {

    }

    @Override
    public void onGetLocalQuestionList(List<QuestionBean> questionBeanList) {

    }

    @Override
    public void onGetQuestionDetail(QuestionResultBean questionResultBean) {
        if (questionResultBean != null) {
            QuestionResultBean.PagingBean paging = questionResultBean.getPaging();
            List<QuestionResultBean.DataBean> dataList = questionResultBean.getData();
            int totalCount = paging.getTotals();
            tv_question_total_answer_count.setText(getString(R.string.app_home_answer_count, totalCount));
            if (dataList != null && dataList.size() > 0) {
                for (QuestionResultBean.DataBean dataBean : dataList) {
                    myAnswerRaking += 1;
                    AuthorBean author = dataBean.getAuthor();
                    authorBeanList.add(author);
                    QuestionResultBean.DataBean.QuestionBean question = dataBean.getQuestion();
                    tv_question_title.setText(getString(R.string.app_home_question_name, question.getTitle()));
                    if (TextUtils.equals(author.getName(), nickName)) {
                        isSuccess = true;
                        tv_my_answer_raking.setText(getString(R.string.question_answer_raking, myAnswerRaking));
                        // 刷新本地数据
                        QuestionBean question1 = DbUtils.getQuestion(questionId);
                        question1.setQuestionAnswerCount(totalCount);
                        question1.setAnswerRaking(myAnswerRaking);
                        DbUtils.saveQuestion(question1);
                        new UIEvent(UIEvent.EVENT_UPDATE_QUESTION_LIST).post();
                        authorListAdapter.notifyDataSetChanged();
                        break;
                    }
                }
            }
            if (!isSuccess) {
                // 没有找到，需要再查找下一页
                if ((page + 1) * CoreConstant.QUESTION_PAGE_COUNT > totalCount) {
                    tv_my_answer_raking.setText(R.string.tip_no_answer_error);
                } else {
                    page += 1;
                    getQuestionDetail();
                }
            }
        }
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
        AuthorBean authorBean = authorBeanList.get(i);
        UserAnswerListActivity.startPage(this, authorBean.getUrl_token());
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {

        return true;
    }
}
